package me.winds.parallax;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import me.winds.testing.R;

/**
 * Auther:  winds
 * Data:    2018/1/10
 * Version: 1.0
 * Desc:
 */


public class Parallax01Fragment extends Fragment {

    ImageView iv_image;
    LinearLayout layout_point;
    int[] images = {R.drawable.guide_01_content, R.drawable.guide_02_content, R.drawable.guide_03_content, R.drawable.guide_04_content};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frragment_parallax_1, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ParallaxViewPager pager = view.findViewById(R.id.pvp_pager);
        iv_image = view.findViewById(R.id.iv_image);
        layout_point = view.findViewById(R.id.layout_point);
        pager.setPageMargin(-400);
        pager.setOverlapPercentage(0.75f);
        pager.setOffscreenPageLimit(2);
        pager.setPageTransformer(true, new ScaleInTransformer(0.65f));
        init(pager);
    }

    void init(ParallaxViewPager pager) {
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.guide_01_image);
        list.add(R.drawable.guide_02_image);
        list.add(R.drawable.guide_03_image);
        list.add(R.drawable.guide_04_image);


        pager.setAdapter(new CommonPagerAdapter<Integer>(list, getActivity(), R.layout.item_parallax) {
            @Override
            protected View bindView(Context context, View contentView, Integer dataVo) {
                ((ImageView) contentView.findViewById(R.id.iv_image)).setImageResource(dataVo);
                return contentView;
            }
        });
        setIndicator(true, 0);
        pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                if (positionOffset != 0) {
                    if (positionOffset <= 0.5) { //左侧
                        iv_image.setImageResource(images[position]);
                        iv_image.setAlpha((1f - positionOffset * 2f));
                        iv_image.setScaleX((1f - positionOffset * 2f));
                        iv_image.setScaleY((1f - positionOffset * 2f));
                    } else { //右侧
                        iv_image.setImageResource(images[position + 1]);
                        iv_image.setAlpha((positionOffset - 0.5f) * 2f);
                        iv_image.setScaleX((positionOffset - 0.5f) * 2f);
                        iv_image.setScaleY((positionOffset - 0.5f) * 2f);
                    }
                }

            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                setIndicator(false, position);
            }
        });
    }

    public void setIndicator(boolean init, int index) {
        if(init) {
            layout_point.removeAllViews();
            for (int i = 0; i < 4; i++) {
                ImageView pointView = new ImageView(getContext());
                pointView.setPadding(8, 0, 8, 0);
                pointView.setImageResource(i == index ? R.drawable.home_ad_dot_pressed : R.drawable.home_ad_dot_normal);
                layout_point.addView(pointView);
            }
        }else {
            for (int i = 0; i < 4; i++) {
                ((ImageView) layout_point.getChildAt(i)).setImageResource(i == index ? R.drawable.home_ad_dot_pressed : R.drawable.home_ad_dot_normal);
            }
        }
    }


}
